package main

import (
	"context"
	"encoding/binary"
	"fmt"
	"github.com/zing-dev/general-protocol-lib/log"
	dts "github.com/zing-dev/general-protocol-lib/source/dts-tcp"
	"github.com/zing-dev/go-bit-bytes/bit"
	"net"
	"net/http"
	"time"
)

func test() {
	server, err := dts.NewServer(context.Background(), &dts.ServerConfig{
		Config: &dts.Config{
			Debug:        true,
			Address:      ":5000",
			Precision:    0.5,
			FilterSignal: true,
			Timeout:      time.Second,
		},
		MQTT: &dts.MQTT{Servers: "10.0.0.20:1883"},
	})
	if err != nil {
		return
	}
	dts.StoreNewCall("s1", dts.CodeLogs, func(l dts.Log) { log.L.Info(l) })
	dts.StoreNewCall("1", dts.CodeClientAdd, func(client *dts.Client) { log.L.Info(client) })
	dts.StoreNewCall("1", dts.CodeClientRemove, func(client *dts.Client) { log.L.Info(client) })
	dts.StoreNewCall("1", dts.CodeClientChangeStatus, func(client *dts.Client) {})
	dts.StoreNewCall("1", dts.CodeClientAllStatus, func(clients []dts.Client) {})
	dts.StoreNewCall("1", dts.CodeFiberAlarm, func(dts.ChannelTemperatureAlarm) {})
	dts.StoreNewCall("1", dts.CodeFiberBrokenAlarm, func(broken dts.ChannelBrokenAlarm) {})
	dts.StoreNewCall("1", dts.CodeConstantTemperatureAlarm, func(temperature dts.ChannelTemperature) {})
	dts.StoreNewCall("1", dts.CodeDifferentialTemperatureAlarm, func(temperature dts.ChannelTemperature) {})
	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		err := server.Exec("000164130015", dts.QUECmdIP())
		if err != nil {
			writer.Write([]byte(err.Error()))
			return
		}
		writer.Write([]byte("ok"))
	})
	go func() {
		err = http.ListenAndServe(":1122", nil)
		if err != nil {
			log.L.Fatalln(err)
		}
	}()
	server.Run()
}

func test2() {
	var listen, err = net.Listen("tcp", ":5000")
	if err != nil {
		log.L.Fatalln(err)
	}

	bit.DefaultEndPont = binary.LittleEndian
	for {
		conn, err := listen.Accept()
		if err != nil {
			log.L.Error(err)
			continue
		}
		log.L.Info("新的连接: ", conn.RemoteAddr())
		_, err = conn.Write([]byte("[Q]?CM#"))
		if err != nil {
			log.L.Error("write: ", err)
		}
		var data = make([]byte, 100)
		n, err := conn.Read(data)
		if err != nil {
			log.L.Info(err)
		}
		log.L.Info(fmt.Sprintf("%x", data[:n]))
	}
}

func test3() {
	client := dts.NewClient(context.Background(), &dts.ClientConfig{
		Name:   "test",
		Server: "10.0.0.220:5000",
		Config: &dts.Config{
			Debug:        true,
			FilterSignal: true,
		},
	})
	dts.StoreNewCall("test", dts.CodeLogs, func(l dts.Log) {
		log.L.Info(l.Log)
	})
	dts.StoreNewCall("test", dts.CodeTemperature, func(l dts.ChannelTemperature) {
		///log.L.DeviceInfo("=> ", len(l.Temperatures))
	})
	dts.StoreNewCall("test", dts.CodeQueryCA, func(params dts.ChannelCalculationParams) {
		log.L.Info("=> ", params)
	})
	time.AfterFunc(time.Second, func() {
		//_ = client.ExecCmd(&cmd.QueryDeviceID{})
		//_ = client.ExecCmd(&cmd.QueryDevice{})
		client.Exec(dts.MRSCmdFW())
		client.Exec(dts.QUECmdCA(0))
		client.Exec(dts.QUECmdCA(1))
		client.Exec(dts.QUECmdCA(2))
	})
	client.Run()
}

func test4() {
	server, err := dts.NewServer(context.Background(), &dts.ServerConfig{
		Config: &dts.Config{
			Debug:        true,
			Address:      "10.0.0.10:5000",
			Precision:    0.5,
			FilterSignal: true,
		},
		MQTT: nil,
	})
	if err != nil {
		log.L.Fatal(err)
	}
	dts.StoreNewCall("server", dts.CodeLogs, func(l dts.Log) {
		log.L.Info(l.Log)
	})
	dts.StoreNewCall("server", dts.CodeClientAdd, func(client *dts.Client) {
		log.L.Info(fmt.Sprintf("设备%s %s", client.Protocol.DeviceCode, client.GetStatus()))
	})
	dts.StoreNewCall("server", dts.CodeTemperature, func(data dts.ChannelTemperature) {
		//fmt.Println(data)
	})

	time.AfterFunc(time.Second*3, func() {
		fmt.Println("start close")
		server.Close()
	})
	server.Run()

	time.Sleep(time.Minute)
}

func main() {
	log.Init()
	test4()
}
